package cn.enilu.flash.api.service;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;

public class WechatPay {

    /** 商户号 */
    public static String merchantId = "1635276169";
    /** 商户API私钥路径 */
    public static String privateKeyPath = "/www/server/tomcat/diandiancuc/flash-waimai-master/flash-waimai-api/src/main/crt/wechatapiclient_key.pem";
    /** 商户证书序列号 */
    public static String merchantSerialNumber = "3D40F93CC5ADDDF7433DBC546FC55CE2D096ADED";
    /** 微信支付平台证书路径 */
    public static String wechatPayCertificatePath = "/www/server/tomcat/diandiancuc/flash-waimai-master/flash-waimai-api/src/main/crt/wechatpay_25A3E7360B60693A3B6A215825E2AFE55A7711E3.pem";
    /** 微信支付 APIv3 密钥 */
    public static String apiV3Key = "abcdefghijklmnopqrstuvwxyz123456";


    
    public String getPrepayId(int price,String title,String orderId,String openId,String notifyUrl) {
        Config config = new RSAConfig.Builder()
                .merchantId(merchantId)
                .privateKeyFromPath(privateKeyPath)
                .merchantSerialNumber(merchantSerialNumber)
                .wechatPayCertificatesFromPath(wechatPayCertificatePath)
                .build();
        JsapiService service = new JsapiService.Builder().config(config).build();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(price);
        request.setAmount(amount);
        request.setAppid("wxac8b1ac7dfe9b329");
        request.setMchid("1635276169");
        request.setDescription(title);
        request.setNotifyUrl(notifyUrl);
        request.setOutTradeNo(orderId);
        Payer payer = new Payer();
        payer.setOpenid(openId);
        request.setPayer(payer);
        PrepayResponse response = service.prepay(request);

        return response.getPrepayId();
    }

}
